using System.Linq;
using Microsoft.AspNetCore.Mvc;
using ToDoListWeb.Service;
using ToDoListWeb.Models;
using Microsoft.AspNetCore.Http;

namespace ToDoListWeb.Controllers
{
    public class InventoryController : Controller
    {
        UserService _user = new UserService();
        TypeService _type = new TypeService();
        InventoryService _inventory = new InventoryService();
        MatterService _matter = new MatterService();

        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 选择清单，以显示清单下所有事项
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ChooseInventory(int inventoryId)
        {
            var matters = _matter.GetMatters(inventoryId);
            return View(matters);
        }
        /// <summary>
        /// 添加清单(w)
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="inventoryName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddInventory(int typeId, string inventoryName)
        {
            if (typeId != 0 && inventoryName != null)
            {
                _inventory.CreateInventory(typeId, inventoryName);
                _user.UpdateUserModel(GetUser());
            }
            return View(GetUser());
        }
        [HttpPost]
        public IActionResult AddInventoryView(int typeId)
        {
            _user.UpdateUserModel(GetUser());
            var aim = _inventory.InventoryToAddInventoryModel(GetUser(), typeId);
            return View(aim);
        }
        /// <summary>
        /// 添加清单(c)
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeId"></param>
        /// <param name="inventoryName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult FoundInventory(int typeId, string inventoryName)
        {
            if (typeId != 0 && inventoryName != null)
            {
                _inventory.CreateInventory(typeId, inventoryName);
                _user.UpdateUserModel(GetUser());
            }
            var type = _type.GetType(typeId);
            return View("ShowInventorys", type);
        }
        /// <summary>
        /// 删除清单
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteInventory(int inventoryId)                                             //删除清单----方法
        {
            _inventory.DeleteIventory(GetUser().UserId, inventoryId);
            _user.UpdateUserModel(GetUser());
            return View("AddInventory", GetUser());
        }
        /// <summary>
        /// 编辑清单（视图）
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        public IActionResult CompileInventoryView(int inventoryId)                                        //编辑清单----视图
        {
            var civ = GetCompileInventoryModel(inventoryId);
            return View(civ);
        }
        /// <summary>
        /// 编辑清单（方法）
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="inventoryId"></param>
        /// <param name="inventoryName"></param>
        /// <returns></returns>
        public IActionResult CompileInventory(int typeId, int inventoryId, string inventoryName)          //编辑清单----方法
        {
            _inventory.CompileInventoryModel(typeId, inventoryId, inventoryName);
            _user.UpdateUserModel(GetUser());
            return View("ShowEvents", GetUser());
        }
        /// <summary>
        /// 创建CompileInventorModel（内部对象）
        /// 用于编辑清单的Model
        /// </summary>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        private CompileInventoryModel GetCompileInventoryModel(int inventoryId)                           //构造CompileInventoryModel----内部方法
        {
            var _aUser = GetUser();
            CompileInventoryModel cim = new CompileInventoryModel()
            {
                User = _aUser,
                Inventory = _aUser.Inventory.SingleOrDefault(i => i.InventoryId == inventoryId)
            };
            return cim;
        }
        /// <summary>
        /// 选择类型后查看该类型下所有清单
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public IActionResult ShowInventorys(int typeId)
        {
            var type = _type.GetType(typeId);
            return View(type);
        }
        public UserModel GetUser()
        {
            var user = _user.GetUserModel((int)HttpContext.Session.GetInt32("user"));
            return user;
        }
    }
}